package com.example.day5_fragment2.fragment;


import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.day5_fragment2.R;

/**
 嵌套
 */
public class ThreeFragment extends Fragment {
    Button bt1,bt2;
    ThreeFragmentOne threeFragmentOne;
    ThreeFragmentTwo threeFragmentTwo;

    public ThreeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_three, container, false);
        bt1 = view.findViewById(R.id.bt1);
        bt2 = view.findViewById(R.id.bt2);
        //默认加载第一个
        FragmentManager manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        if(threeFragmentOne != null){
            transaction.show(threeFragmentOne);
        }else{
            threeFragmentOne = new ThreeFragmentOne();
            transaction.add(R.id.frame,threeFragmentOne);
        }
        transaction.commit();
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //切换fragment
                hideAll();
                FragmentManager manager = getChildFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                if(threeFragmentOne != null){
                    transaction.show(threeFragmentOne);
                }else{
                    threeFragmentOne = new ThreeFragmentOne();
                    transaction.add(R.id.frame,threeFragmentOne);
                }
                transaction.commit();


            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//切换fragment
                  hideAll();
                FragmentManager manager = getChildFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                if(threeFragmentTwo != null){
                    transaction.show(threeFragmentTwo);
                }else{
                    threeFragmentTwo = new ThreeFragmentTwo();
                    transaction.add(R.id.frame,threeFragmentTwo);
                }
                transaction.commit();
            }
        });
        return view;
    }

    public void hideAll(){
        FragmentManager manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        if(threeFragmentOne != null){
            transaction.hide(threeFragmentOne);
        }
        if(threeFragmentTwo != null){
            transaction.hide(threeFragmentTwo);
        }
        transaction.commit();
    }

}
